package com.capgemini.diabeo.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.capgemini.diabeo.manager.MealStatus;
import com.capgemini.diabeo.manager.MenuStatus;
import com.capgemini.diabeo.model.Dish;
import com.capgemini.diabeo.model.DishDao;
import com.capgemini.diabeo.model.Menu;
import com.capgemini.diabeo.model.MenuDao;
import com.capgemini.diabeo.model.MenuDish;
import com.capgemini.diabeo.model.MenuDishDao;
import com.capgemini.diabeo.model.MenuDao.Properties;
import com.googlecode.androidannotations.annotations.EBean;

@EBean
public class MenuService extends AbstractDatabaseService {

	public Menu retrieveCurrentMenu(MealStatus dishListStatus) {
		MenuDao menuDao = dbHelper.getMenuDao();
		List<Menu> menus = menuDao
				.queryBuilder()
				.where(Properties.MealTime.eq(dishListStatus.getValue()),
						Properties.Status.eq("current")).list();
		Menu menu = null;
		if (menus != null && !menus.isEmpty()) {
			menu = menus.get(0);
		}
		return menu;
	}
	
	public List<Menu> retrieveHistoryMenu(){
		MenuDao menuDao = dbHelper.getMenuDao();
		List<Menu> menuList = menuDao.queryBuilder().build().list();
		
		return menuList;
	}

	public Menu createNewMenu(MealStatus mealEnum) {
		Menu menu = new Menu();
		menu.setMealTime(mealEnum.getValue());
		menu.setStatus(MenuStatus.CURRENT.getValue());
		menu.setCreationDate(new Date());
		menu.setLastChange(new Date());
		MenuDao menuDao = dbHelper.getMenuDao();
		menuDao.insert(menu);
		return menu;
	}

	public void update(Menu menu) {
		menu.setLastChange(new Date());
		MenuDao menuDao = dbHelper.getMenuDao();
		menuDao.update(menu);
	}
}
